package com.woniuxy.web.back;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.woniuxy.entity.*;
import com.woniuxy.service.DishService;
import com.woniuxy.service.OrderService;
import com.woniuxy.service.VDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@JsonIgnoreProperties("handler")
@CrossOrigin(value="http://192.168.6.186:8080",allowCredentials = "true")
//统一前缀为user
@RequestMapping("admin/dish")
public class DishController_b {
    @Autowired
    private DishService dishService;

    /**
     * 分页展示
     * @param current
     * @param pageSize
     * @param dish
     * @return
     */
    @GetMapping("pageList")
    public ResponseEntity<List<Dish>> list(Integer current, Integer pageSize, Dish dish){
        if(current ==null || current == 0){
            current=1;
        }
        if(pageSize == null || pageSize == 0){
            pageSize=2;
        }
        //条件筛选
        QueryWrapper<Dish> queryWrapper =new QueryWrapper<>();
        if(dish.getDishName() !=null && dish.getDishName().trim().length() !=0){
            queryWrapper.like("name",dish.getDishName());
        }

        queryWrapper.eq("status","y");
        Page<Dish> page=new Page<>(current,2);
        page=dishService.page(page,queryWrapper);

        return new ResponseEntity(page);
    }

    /**
     * 根据id获取到选择到的行
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public ResponseEntity<Dish> get(@PathVariable int id){
        return new ResponseEntity<>(dishService.getById(id));
    }

    /**
     * 修改商品信息
     * @param dish
     * @return
     */
    @PostMapping ("update")
    public ResponseEntity<?> update(@RequestBody Dish dish){

        dish.setStatus("y");
        dishService.update(dish);
        return ResponseEntity.SUCCESS;
    }

    /**
     * 删除对应id的商品
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public ResponseEntity del(@PathVariable int id) {
        dishService.removeById(id);
        return ResponseEntity.SUCCESS;
    }
   /**
   * @description:
    * @return: com.woniuxy.entity.ResponseEntity<java.util.List<com.woniuxy.entity.User>>
    * @date: 2021/8/12 15:32
    * @author: 魏炳豪
    */

    @GetMapping("list/{id}")
    public ResponseEntity<List<Dish>> list(@PathVariable Integer id){
       List<Dish> dishs = dishService.getByOrderId(id);
        return new ResponseEntity<List<Dish>>(dishs);
    }

    @PostMapping("add")
    public void add(@RequestBody Dish dish){
        dish.setPicture(imageName);
        System.out.println(imageName);
        System.out.println(imagePath);
        dishService.add(dish);
    }

    @Value("${gfr.images}")
    private String imagePath;

    private String imageName;

    @PostMapping("addPicture")
    public ResponseEntity addPicture(@RequestParam MultipartFile file){
        String originaFilename = file.getOriginalFilename();
        //获取文件上传后缀
        String suffix = originaFilename.substring(originaFilename.lastIndexOf("."));
        //产生新文件名
        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
        imageName=fileName;
        try {
            //写入服务器端
            File file1 = new File(imagePath);
            file1.mkdir();
            file.transferTo(new File(imagePath + "\\" + imageName));

        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.FIAL;
        }
        return ResponseEntity.SUCCESS;
    }

}
